package com.mitcub.panda.controller;

import com.mitcub.panda.base.BaseController;
import com.mitcub.panda.entity.Order;
import com.mitcub.panda.query.OrderQuery;
import com.mitcub.panda.service.IOrderService;
import com.mitcub.panda.utils.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @项目名称: panda
 * @类名称: PayOrderController
 * @类描述: 缴费系统 支付订单 控制器
 * @作者: tangloong
 * @创建时间: 2021/11/9 14:45
 */

@Controller
@RequestMapping("/payment/order")
public class PayOrderController extends BaseController {
    @Autowired
    IOrderService orderService;

    @ResponseBody
    @RequestMapping("/list")
    public RespResult list(OrderQuery query) {
        return orderService.getList(query);
    }

    /**
     * 编辑 新增 记录详情
     * @param id    记录ID
     * @param model 模型
     * @return
     */
    @Override
    public String edit(Long id, Model model) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = orderService.info(id);
        }
        model.addAttribute("info", info);
        return super.edit(id, model);
    }

    /**
     * 添加记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public RespResult add(@RequestBody Order entity) {
        return orderService.edit(entity);
    }

    /**
     * 修改记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public RespResult update(@RequestBody Order entity) {
        return orderService.edit(entity);
    }



    /**
     * 删除记录
     * @param id 记录ID
     * @return
     */
    @ResponseBody
    @GetMapping("/delete/{id}")
    public RespResult delete(@PathVariable("id") Long id) {
        return orderService.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids 记录ID(多个使用逗号","分隔)
     * @return
     */
    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    public RespResult batchDelete(@PathVariable("ids") String ids) {
        return orderService.deleteByIds(ids);
    }

    /**
     * 设置状态
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/setStatus")
    public RespResult setStatus(@RequestBody Order entity) {
        Order order = new Order();
        order.setId(entity.getId());
        order.setStatus(entity.getStatus());
        return orderService.setStatus(order);
    }

}
